package com.cjm.thread.zhonjianjian;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class Inter {
    private static AtomicBoolean run = new AtomicBoolean(true);
    public static void main(String[] args) throws InterruptedException {
        m1();
    }

    private static void m1() {
        AtomicInteger num = new AtomicInteger();
        Thread t = new Thread(() -> {
            while (true) {
                num.getAndAdd(1);
            }
//            System.err.println("线程t， 结束, 中断状态为" + Thread.currentThread().isInterrupted());
        }, "cjm - thread ");
        t.start();

        Thread t2 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            System.err.println("线程t1， 是否中断？" + t.isInterrupted());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            t.interrupt();
            System.err.println("线程t1， 是否中断？" + t.isInterrupted());
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            shutdown();
            System.err.println("num = " + num.get());
        }, "main method");
        t2.start();

        System.err.println("main over.");
    }

    public static void shutdown() {
        run.set(false);
    }
}
